/**
 * Created by wlc on 2018/5/8.
 */
/**
 * 页面初始化
 */
window.onload = function (){
    var inputs;
    var tbodyTRs;

    //判断消息是否已读
    inputs = document.getElementById("message-mark-isread").children;
    for (var i = 0;i<inputs.length;i++){
        if (inputs[i].value === "1"){
            var tr = document.getElementById(inputs[i].name);
            tr.className += " read";
        }
    }
    //显示剩余部分消息
    //全部消息面板
    tbodyTRs = document.getElementById("allType-message-tbody").children;
    for (var i = 0;i < tbodyTRs.length;i++){
        //显示不同类型消息
        if (tbodyTRs[i].children[4].innerText.indexOf("权限") !== -1){
            document.getElementById("authority-message-tbody").innerHTML += tbodyTRs[i].outerHTML;
        }
        if (tbodyTRs[i].children[4].innerText.indexOf("反馈") !== -1){
            document.getElementById("feedback-message-tbody").innerHTML += tbodyTRs[i].outerHTML;
        }
        //显示未读已读消息
        if (document.getElementsByName(tbodyTRs[i].id)[0].value === "0"){
            document.getElementById("allType-unreadMessage-tbody").innerHTML += tbodyTRs[i].outerHTML;
        }
        if (document.getElementsByName(tbodyTRs[i].id)[0].value === "1"){
            document.getElementById("allType-readMessage-tbody").innerHTML += tbodyTRs[i].outerHTML;
        }

    }
    //未读消息面板
    tbodyTRs = document.getElementById("allType-unreadMessage-tbody").children;
    for (var i = 0;i < tbodyTRs.length;i++){
        if (tbodyTRs[i].children[4].innerText.indexOf("权限") !== -1){
            document.getElementById("authority-unreadMessage-tbody").innerHTML += tbodyTRs[i].outerHTML;
        }
        if (tbodyTRs[i].children[4].innerText.indexOf("反馈") !== -1){
            document.getElementById("feedback-unreadMessage-tbody").innerHTML += tbodyTRs[i].outerHTML;
        }
    }
    //已读消息面板
    tbodyTRs = document.getElementById("allType-readMessage-tbody").children;
    for (var i = 0;i < tbodyTRs.length;i++){
        if (tbodyTRs[i].children[4].innerText.indexOf("权限") !== -1){
            document.getElementById("authority-readMessage-tbody").innerHTML += tbodyTRs[i].outerHTML;
        }
        if (tbodyTRs[i].children[4].innerText.indexOf("反馈") !== -1){
            document.getElementById("feedback-readMessage-tbody").innerHTML += tbodyTRs[i].outerHTML;
        }
    }
};

/**
 * 单击事件监听
 * @param e
 */
document.onclick = function (e) {
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素
    //设置消息选中
    if (tar.id.indexOf("message-selectAll") !== -1){
        var checkbox = document.getElementsByName(tar.id);
        if (tar.checked === true){
            for (var i = 0;i< checkbox.length;i++){
                checkbox[i].checked = true;
            }
        }else {
            for (var i = 0;i< checkbox.length;i++){
                checkbox[i].checked = false;
            }
        }
    }
    //设置消息显示
    if (tar.className === "message-show"){
        var messgaeId = tar.parentNode.parentNode.id;
        $.ajax({
            type: 'GET',
            url: '../messageManager/getMessage',
            data:{messageId:messgaeId},
            success: function (returndata) {
                var content = returndata.split("wlcniubi");
                document.getElementById("messageShow-id").value = content[0];
                document.getElementById("message-title").children[0].innerHTML = content[5];
                document.getElementById("message-sender").children[0].innerHTML = '<span>发件人：</span>'+content[4]+"("+content[3]+")";
                document.getElementById("message-date").children[0].innerHTML = '<span>时 间：</span>'+content[6];
                document.getElementById("message-content").children[0].innerHTML = content[8];

            }
        });
        $.ajax({
            type:"GET",
            url:"../messageManager/getMessageImg",
            data:{messageId:messgaeId},
            success:function (returndata) {

                var imgInfo = JSON.parse(returndata);
                console.log(imgInfo);

                var src = '';
                for(var p in imgInfo) {
                    console.log(imgInfo[p]);
                    src = "../messageManager/showImage/" + imgInfo[p].userId + "/" + imgInfo[p].messageId + "/" + imgInfo[p].ImgName;
                    console.log(src);
                    $('#message-image').prepend("<img width='540' height='540' src=" + src + "/>");
                }
                // $('#message-image').prepend("<br>");
                $('#message-image').prepend("<span><p>附件：</p></span>")
            }
        });

        $('#messageShow').modal();
    }
};

/**
 * 删除被选中消息
 * @param tbodyid
 */
function selectedMessageDel(tbodyid) {
    var trs = document.getElementById(tbodyid).children;
    var ids = "";

    for (var i = 0;i<trs.length;i++){
        if (trs[i].children[0].children[0].checked === true){
            ids += trs[i].id + "wlcniubi"
        }
    }
    if (!isEmpty(ids)){
        $.ajax({
            type: 'POST',
            url: '../messageManager/delMessages',
            data:{messageIds:ids},
            success: function (returndata) {
                if (returndata === "true"){
                    window.location.reload();
                }
            }
        });
    }else {
        alert("请选择要删除消息！");
    }
}

/**
 * 将被选中消息标记为已读
 * @param tbodyid
 */
function selectedMessageReaded(tbodyid) {
    var trs = document.getElementById(tbodyid).children;
    var ids = "";

    for (var i = 0;i<trs.length;i++){
        if (trs[i].children[0].children[0].checked === true && document.getElementsByName(trs[i].id)[0].value === "0"){
            ids += trs[i].id + "wlcniubi"
        }
    }
    if (!isEmpty(ids)){
        $.ajax({
            type: 'POST',
            url: '../messageManager/markMessages',
            data:{messageIds:ids,markStuatus:"0"},
            success: function (returndata) {
                if (returndata === "true"){
                    window.location.reload();
                }
            }
        });
    }else {
        alert("请选择未读的消息！");
    }
}

/**
 * 对话框结束操作
 */
$('#messageShow').on('hidden.bs.modal', function (e) {
    var id = document.getElementById("messageShow-id").value + "wlcniubi";
    $.ajax({
        type: 'POST',
        url: '../messageManager/markMessages',
        data:{messageIds:id,markStuatus:"0"},
        success: function (returndata) {
            if (returndata === "true"){
                window.location.reload();
            }
        }
    });
});

/**
 * 对话框开始操作
 */
$('#messageShow').on('show.bs.modal', function (event) {
    //        获取用户id
    var user_infor_start = document.cookie.indexOf("GenomeDatabase_user_infor=")+"GenomeDatabase_user_infor=".length;
    var user_infor_end = document.cookie.indexOf(";",user_infor_start);
    if (user_infor_end===-1) {
        user_infor_end=document.cookie.length;
    }
    //显示回复框
    if(unescape(document.cookie.substring(user_infor_start,user_infor_end)).split("wlcniubi")[0] === "0"){
        var modal_footer = $('#messageShow')[0].children[0].children[0].children[2];
        modal_footer.innerHTML = '<div class="form-group" id="Message-Reply" style="height: 110px;"> <label class="col-sm-2 control-label">内容：</label> <div class="col-sm-10"> <textarea class="form-control" name="content" rows="5"></textarea> </div> </div>'
        modal_footer.innerHTML += '<div class="form-group" id="Message-Reply-submit"> <label class="col-sm-2 control-label suggestion-template-title"></label> <div class="col-sm-10"> <button type="button"  class="btn btn-primary" onclick="replyMessage()">回复</button> </div> </div>'
    }
});

/**
 * 用于消息回复
 */
function replyMessage() {
    var content = $('#Message-Reply')[0].children[1].children[0].value;
    if (!isEmpty(content)){
        var receiver = $('#message-sender')[0].children[0].textContent.split("：")[1];
        var receiverId = receiver.split("(")[1].slice(0,-1);
        var receiverName = receiver.split("(")[0];
        var title = "re:"+ $('#message-title')[0].children[0].textContent;
        $.ajax({
            type: 'POST',
            url: '../messageManager/replyMessage',
            data:{receiverId:receiverId,receiverName:receiverName,title:title,content:content},
            success: function (returndata) {
                if (returndata === "true"){
                    var id = document.getElementById("messageShow-id").value + "wlcniubi";
                    $.ajax({
                        type: 'POST',
                        url: '../messageManager/markMessages',
                        data:{messageIds:id,markStuatus:"0"},
                        success: function (returndata) {
                            if (returndata === "true"){
                                window.location.reload();
                            }
                        }
                    });
                }
            }
        });
    }else {
        alert("请填写消息！");
    }
}

var flag_img = false;
$('#message-image img').click(function () {
    var im = $('#message-image img');
    if(flag_img === false) {
        im.height(270);
        im.width(320);
        console.log(flag_img);
        flag_img = true;
    } else {
        im.height(40);
        im.width(40);
        flag_img = false;
    }
});